import { getProduct } from "../api"
export default {
    namespaced: true,
    state: {
        name: "product",
        products: []
    },
    mutations: {
        //设置购物车数据
        SET_PRODUCT(state, payload) {
            state.products = payload
        },
        //减少库存
        DECREMENT_INVENTORY(state, product_id) {
            console.log(product_id)
            //根据product_id找到state.products中的数据
            let target = state.products.find(item => item.id === product_id)
            if (target) {
                target.inventory--
            }
        }
    },
    getters: {},
    // modules: {}
    actions: {
        //获取购物数据
        FETCH_PRODUCT({ commit }) {
            getProduct()
                .then(res => {
                    commit('SET_PRODUCT', res.data)
                })
        }
    }
}